package com.cgw.room.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.cgw.room.DogControllerActivity;
import com.cgw.room.MainApp;
import com.cgw.room.R;

public class AccButton extends View {
    private static final int ACCELERATION_RANK = 5;
    private int mCurrentRank = 1;

    DogControllerActivity mActivity = null;

    private String ButtonName = "";

    private boolean mPressed = false;
    private long mPressedTimestamp = 0;


    public AccButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        mActivity = (DogControllerActivity)context;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Rect rc = new Rect();
        this.getDrawingRect(rc);
        RectF rcF = new RectF();
        rcF.set(rc);

        float rx = 40;
        float ry = 40;
        if (mPressed){
            int colorSelected = Color.parseColor("#1AB6FF");
            Paint paintSelected = new Paint();
            paintSelected.setColor(colorSelected);
            canvas.drawRoundRect(rcF, rx, ry, paintSelected);
        }else{
            int colorButton = Color.parseColor("#555555");
            Paint paintButton = new Paint();
            paintButton.setColor(colorButton);
            canvas.drawRoundRect(rcF, rx, ry, paintButton);
        }

        int TextSize = 50;
        int TextColor = Color.parseColor("#FFFFFF");
        Paint paintText = new Paint();
        paintText.setTextSize(TextSize);
        paintText.setColor(TextColor);
        int szWidth = (int)(paintText.measureText(ButtonName));
        int startX = (this.getWidth() - szWidth) / 2;
        int startY = (this.getHeight() + TextSize) / 2;
        canvas.drawText(ButtonName, startX, startY, paintText);
        Log.i("[ACC]", ButtonName + "   " + startX + "   " + startY);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int eventMask = event.getActionMasked();
        if (eventMask == MotionEvent.ACTION_DOWN){
            mPressed = true;
            mPressedTimestamp = System.currentTimeMillis();
            invalidate();
        }
        else if (eventMask == MotionEvent.ACTION_MOVE){
            if (mPressed){
                long ts = System.currentTimeMillis();
                // TODO_MOYANG, 1000ms = 1s
                mCurrentRank = (int)((ts - mPressedTimestamp) / 1000) + 1;
                if (mCurrentRank > ACCELERATION_RANK)
                    mCurrentRank = ACCELERATION_RANK;

//                Log.i("[PRESS]", "mCurrentRank = " + mCurrentRank + " ts = " + ts + " mPressedTimestamp = " + mPressedTimestamp);
                invalidate();
            }
        }
        else if (eventMask == MotionEvent.ACTION_UP){
            mPressed = false;
            invalidate();
        }
        return true;
    }
    public void setButtonName(String buttonName){
        this.ButtonName = buttonName;
    }
    public boolean isMotion(){
        return mPressed;
    }

    public int getRank(long currtentTimestamp){
        mCurrentRank = (int)((currtentTimestamp - mPressedTimestamp) / 500) + 1; // 2025/9/2 change from 1000 to 500 to speed up
        if (mCurrentRank > ACCELERATION_RANK)
            mCurrentRank = ACCELERATION_RANK;

        return mCurrentRank;
    }
}
